package com.globallogic.training.javase.basics;

import java.util.Scanner;
import java.util.Arrays;

/**
* This program receives the input of two strings
* and gives the output to the screen telling whether these words are anagrams.
* @version 1.2 2013-04-05
* @author Ivan Moisiienko
*/

public class Anagram {

	static final String ANAGRAM_OUTPUT_MESSAGE
		= "The input strings are anagrams.";
	static final String NOT_ANAGRAM_OUTPUT_MESSAGE
		= "The input strings are not anagrams.";
	static final String FIRST_PHRASE
		= "Input first word or phrase: ";
	static final String SECOND_PHRASE
		= "Input second word or phrase: ";

	public static void main(String[] args) {

		String phrase1 = provideInput(FIRST_PHRASE);
		String phrase2 = provideInput(SECOND_PHRASE);

		boolean isAnagram = isAnagram(phrase1, phrase2);

		printResult(isAnagram);

	}

	static String provideInput(String s) {
		Scanner in = new Scanner(System.in);
		System.out.print(s);
		String result = in.nextLine().trim().replaceAll("\\s", "");
		return result;
	}

	static boolean isAnagram(String phrase1, String phrase2) {

		if (phrase1.length() == 0
				|| phrase2.length() == 0
				|| phrase1.length() != phrase2.length()) {
			return false;
      	}

		char[] firstPhrase = phrase1.toLowerCase().toCharArray();
		char[] secondPhrase = phrase2.toLowerCase().toCharArray();

		Arrays.sort(firstPhrase);
		Arrays.sort(secondPhrase);

		if (!Arrays.equals(firstPhrase, secondPhrase)) {
			return false;
		}

		return true;
	}

	static void printResult(boolean isAnagram) {
		if (isAnagram) {
			System.out.println(ANAGRAM_OUTPUT_MESSAGE);
		} else {
			System.out.println(NOT_ANAGRAM_OUTPUT_MESSAGE);
		}
	}

}